<!--
 * @Author: zhangpeng
 * @Date: 2024-11-14 17:13:09
 * @FilePath: \scientific_research_manager_web\src\views\project\mine\submitConclusion.vue
 * @LastEditTime: 2025-01-10 15:16:29
 * @Description: 提交结项
-->
<template>
  <div class="submit-conclusion" v-loading="loading">
    <MyProjectHeader :project-id="projectId" :project-name="projectName" />

    <div class="submit-conclusion-content">
      <el-form :model="formDate" label-width="auto" class="mt-[20px]" :rules="rules" ref="formRef">
        <el-form-item label="结项文件" required>
          <FileUpload :is-show-tip="false" drag width="393px" @update:modelValue="update" :model-value="formDate.fileList"/>
        </el-form-item>

        <el-form-item label="相关成果" prop="achievementIds">
          <el-select v-model="formDate.achievementIds" placeholder="请输入关键字" multiple filterable clearable
            style="width: 400px;">
            <el-option :label="i?.name" :value="i?.id" v-for="i in achieveList" :key="i.id" />
          </el-select>
        </el-form-item>
      </el-form>

      <div class="h-[72px] bg-white"></div>
      <div class="examine-bar">
        <el-button @click="proxy.$tab.closePage()" v-if="route.query.type !== 'edit'">取消</el-button>
        <el-button type="primary" @click="submit">{{ route.query.type === 'edit' ? '保存' : '提交结项'}}</el-button>
      </div>

    </div>
  </div>
</template>
<script lang="ts">
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    if(to.query.type === 'edit'){
      to.meta.title = '编辑结项';
    }
    
    next()
  }
})
</script>

<script setup lang="ts" name="SubmitConclusion">
import { ref } from 'vue';
import { getApproveMaterialDetailsApi, projectApproveSubmitApi } from '@/api/project'
import { getResultsCommonListApi } from '@/api/resultsManagement/index'
import { AchvInfoVO } from '@/api/resultsManagement/types'
import { projectStatusEnum, projectSubmitTypeEnum } from '@/utils/constEnum'
import MyProjectHeader from '../components/MyProject/MyProjectHeader.vue'
import useUserStore from '@/store/modules/user';
import { ProjectMaterialDetailVO } from '@/api/project/types';

const { proxy } = getCurrentInstance();
const route = useRoute()
const userStore = useUserStore()
const projectId = ref(route.query.projectId as unknown as string)
const projectName = ref(route.query.projectName as unknown as string)
const fileIds = computed(() => {
  return formDate.value?.fileList?.map((i) => i?.id)
})
const achieveList = ref<AchvInfoVO[]>([])
const formRef = ref()
const formDate = ref({
  fileList: [], //  结项文件
  achievementIds: [], //  成果ids
})
const createById = ref('')
const rules = {
  achievementIds: [{ required: true, message: "相关成果不能为空", trigger: "change" }],
}
// 文件变化
function update(val) {
  formDate.value.fileList = val
}

// 获取成果list
const { run: getResultsCommonList } = useRequest(getResultsCommonListApi, {
  manual: true,
  debounceWait: 500,
  onSuccess: (res: AchvInfoVO[]) => {
    achieveList.value = res
  }
})

const { runAsync: getDetail } = useRequest(() => getApproveMaterialDetailsApi({
  materialType: 3,
  projectId: projectId.value
}), {
  manual: true,
  onSuccess: (res: ProjectMaterialDetailVO) => {
    formDate.value.achievementIds = res?.achievementDTOList?.map(i => i.achievementId) || []
    formDate.value.fileList = res?.attachmentFileInfoDTOList
    createById.value = res?.createdBy
  }
})

// 提交结项
const { run: projectApproveSubmit, loading } = useRequest(projectApproveSubmitApi, {
  manual: true,
  debounceWait: 500,
  onSuccess: (res) => {
    if (res?.isSuccess) {
      proxy.$modal.msgSuccess(route.query.type === 'edit' ? "保存成功" : "提交成功");
      close()
    }
  }
})

function close() {
  proxy.$tab.closePage();
}

async function submit() {
  await formRef.value?.validate((valid) => {
    if (valid) {
      if (!formDate.value.fileList?.length) {
        ElMessage.error('结项文件不能为空')
        return
      }
      // 结项评审待通过时，type = 27
      const status = Number(route.query.status || -1)
      projectApproveSubmit({
        achievementIds: formDate.value?.achievementIds,
        projectId: projectId.value,
        fileIds: fileIds.value,
        type: status === projectStatusEnum.changeWaitApproval ? projectSubmitTypeEnum.submitFailed : projectSubmitTypeEnum.submitConclusion,
      })
    }
  })
}

onMounted(async() => {
  await getDetail({ projectId: projectId.value })
  console.log(1)
  getResultsCommonList({
    createdBy: route.query.type === 'edit' ? createById.value : userStore?.id,
    status: 5,  //  已登记(完全审核通过)
  })
})

</script>

<style lang="scss" scoped>
.submit-conclusion {
  --at-apply: bg-white;
  min-height: 100%;

  &-content {
    --at-apply: p-[20px];
  }

}

.examine-bar {
  border-top: 1px solid #E5E6EB;
  --at-apply: fixed bottom-0 left-0 right-0 h-[72px] flex items-center justify-center bg-white z-1999;

  :deep(.el-button) {
    height: 32px;
  }
}
</style>